<?php

/**
 * This is the model class for table "article_cat".
 *
 * The followings are the available columns in table 'article_cat':
 * @property integer $cat_id
 * @property string $cat_name
 * @property integer $p_id
 * @property string $keywords
 * @property string $desc
 * @property integer $sort_order
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Article[] $articles
 */
class ArticleCat extends CommonAR
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return ArticleCat the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function getResourceName()
	{
		return '文章分类';
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'article_cat';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('p_id, sort_order, status', 'numerical', 'integerOnly'=>true),
			array('cat_name', 'length', 'max'=>50),
			array('keywords', 'length', 'max'=>100),
			array('desc', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('cat_id, cat_name, p_id, keywords, desc, sort_order, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'articles' => array(self::HAS_MANY, 'Article', 'cat_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'cat_id' => '分类',
			'cat_name' => '分类名称',
			'p_id' => '上级分类',
			'keywords' => '关键词',
			'desc' => '页面描述',
			'sort_order' => '排序',
			'status' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('cat_id',$this->cat_id);
		$criteria->compare('cat_name',$this->cat_name,true);
		$criteria->compare('p_id',$this->p_id);
		$criteria->compare('keywords',$this->keywords,true);
		$criteria->compare('desc',$this->desc,true);
		$criteria->compare('sort_order',$this->sort_order);
		$criteria->compare('status',$this->status);
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getCatOptionsHash($level=false, $pid=0, $hideNotActive=false, $margin=0)
	{
		if ($level>=0 || $level===false) {
			$filer= array('p_id'=>$pid);
			if ($hideNotActive===true) {
				$filer = array_merge($filer, array('status'=>1 ) );
			}
			
			$result= $this->findAllByAttributes($filer );
			if (count($result)>0) {
				$options= array();
				foreach ($result as $v) {
					$key= (int) $v->getAttribute('cat_id');
					$options[$key] = str_repeat('|---', (int) $margin ). $v->getAttribute('cat_name');
					$_level = $level===false? $level: $level-1;
					$_pid = $v->getAttribute('cat_id');
					$tmp = $this->getCatOptionsHash($_level, $_pid, $hideNotActive, $margin+1);
					if ( count($tmp) >0 ) {
						$options= $options+ $tmp;
					}
				}
				return $options;
			}
			else {
				return array();
			}
		} else {
			return array();
		}
	}
	
}